﻿using AutoMapper;
using Mapster;
using MediatR;
using ShineTech.Domain.Models.Kanbans;
using ShineTech.Domain.Models.Missons;
using ShineTech.Domain.SeedWork;
using ShineTech.Web.Missons.Commands;
using System.Threading;
using System.Threading.Tasks;


namespace ShineTech.Web.Missons
{
    public class MissonCommandHandler :
        IRequestHandler<CreateMissonCommand, MissonDTO>,
        IRequestHandler<UpdateMissonCommand, MissonDTO>,
        IRequestHandler<CompletedMissonCommand, MissonDTO>,
        IRequestHandler<MissonByIdQuery, MissonDTO>,
        IRequestHandler<MissonsByFiltersPageQuery, PageData<MissonDTO>>,
        INotificationHandler<DeleteMissonCommand>

    {
        private readonly IUnitOfWork _uow;
        private readonly IMissonRepository _MissonRepository;
        private readonly IShareRepository _ShareRepository;
        private readonly IMapper _mapper;

        public MissonCommandHandler(IMissonRepository missonRepository, IShareRepository shareRepository, IUnitOfWork uow, IMapper mapper)
        {
            _MissonRepository = missonRepository;
            _ShareRepository = shareRepository;
            _uow = uow;
            _mapper = mapper;
        }

        /// <summary>
        /// 在看板中建立任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<MissonDTO> Handle(CreateMissonCommand request, CancellationToken cancellationToken)
        {
            Misson misson = new Misson(request.CreatedTime, request.Name, request.CreatedUserId, request.CreatedUserName,request.KanbanId);            
            _MissonRepository.Add(misson);
            await _uow.CommitAsync(cancellationToken);
            return _mapper.Map<MissonDTO>(misson);
        }
        /// <summary>
        /// 查看任务详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<MissonDTO> Handle(MissonByIdQuery request, CancellationToken cancellationToken)
        {
            Misson misson = await _MissonRepository.FindOneAsync(new MissonByIdSpecification(request.Id));
            return _mapper.Map<MissonDTO>(misson);
        }
        /// <summary>
        /// 分页查看所有任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageData<MissonDTO>> Handle(MissonsByFiltersPageQuery request, CancellationToken cancellationToken)
        {
            
            var missons = await _MissonRepository.GetPageData(
                new MissonsByFiltersSpecification(request.Name ?? string.Empty,
                    request.KanbanId,
                    request.IsComplete),
                request.PageIndex,
                request.PageSize);
            return missons.Adapt<PageData<MissonDTO>>();
        }
       

        /// <summary>
        /// 完成任务        
        ///  </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<MissonDTO> Handle(CompletedMissonCommand request, CancellationToken cancellationToken)
        {

            Misson misson = await _MissonRepository.FindOneAsync(new MissonByIdSpecification(request.Id));
            if (misson == null)
                throw CustomException.NotFound("任务");
            misson.Complete();
            _MissonRepository.Update(misson);
            await _uow.CommitAsync(cancellationToken);
            return _mapper.Map<MissonDTO>(misson);
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<MissonDTO> Handle(UpdateMissonCommand request, CancellationToken cancellationToken)
        {

            Misson misson = await _MissonRepository.FindOneAsync(new MissonByIdSpecification(request.Id));
            if (misson == null)
                throw CustomException.NotFound("任务");
            misson.UpdateName(request.Name);
            _MissonRepository.Update(misson);
            await _uow.CommitAsync(cancellationToken);
            return _mapper.Map<MissonDTO>(misson);
        }

      

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(DeleteMissonCommand notification, CancellationToken cancellationToken)
        {

            Misson misson = await _MissonRepository.FindOneAsync(new MissonByIdSpecification(notification.Id));
            if (misson == null)
                throw CustomException.NotFound("misson");
            _MissonRepository.Remove(misson);

            await _uow.CommitAsync(cancellationToken);
        }
    }
}
